import { baseModule } from "@/base/module";
import { LazyLoader } from "@sapi-game/gameComponent/common/lazyLoader";
import { RegionTeamChooser } from "@sapi-game/gameComponent/region/regionTeamChooser";
import { GamePlayer } from "@sapi-game/gamePlayer/gamePlayer";
import { PlayerGroup } from "@sapi-game/gamePlayer/playerGroup";
import { CubeRegion, SphereRegion } from "@sapi-game/gameRegion/gameRegion";
import { Game, RegionTeamCleaner, TeamScoreBoard } from "@sapi-game/main";
import { DimensionIds } from "@sapi-game/utils/vanila-data";
import { CvSGame } from "../game";
import { CvSMap, CvSMaps } from "../map";
import { CvSMapChooser } from "./mapChooser";
import { CvSStarter } from "./starter";

export class CvSBackState extends baseModule.State {
    readonly chicken = new PlayerGroup(GamePlayer);
    readonly shepherd = new PlayerGroup(GamePlayer);
    map: CvSMap = CvSMaps.classic;

    override onEnter(): void {
        //队伍选择
        this.addComponent(LazyLoader, {
            onLoad: this.onLoad.bind(this),
            onUnload: this.onUnload.bind(this),
            dimensionId: DimensionIds.Overworld,
            pos: { x: 34, y: -61, z: 49 },
        });
    }

    onLoad(loader: LazyLoader) {
        loader
            .addComponent(TeamScoreBoard, {
                scoreboardName: "CvS_team",
                displayName: "§a鸡王争霸-选队",
                teams: [
                    { team: this.chicken, prefix: "§c[鸡]§r" },
                    { team: this.shepherd, prefix: "§b[猎]§r" },
                ],
            })
            .addComponent(CvSStarter)
            .addComponent(CvSMapChooser);
        const score = this.getComponent(TeamScoreBoard);
        loader.addComponent(RegionTeamChooser, {
            config: [
                {
                    team: this.chicken,
                    region: new CubeRegion(
                        DimensionIds.Overworld,
                        { x: 38, y: -61, z: 61 },
                        { x: 36, y: -59, z: 59 }
                    ),
                    onJoin: (p) => {
                        p.sendMessage("§c你加入了小鸡队伍");
                        score.show();
                        score.refreshScoreBoard();
                    },
                },
                {
                    team: this.shepherd,
                    region: new CubeRegion(
                        DimensionIds.Overworld,
                        { x: 32, y: -61, z: 61 },
                        { x: 30, y: -59, z: 59 }
                    ),
                    onJoin: (p) => {
                        p.sendMessage("§b你加入了猎人队伍");
                        score.show();
                        score.refreshScoreBoard();
                    },
                },
            ],
        });
        loader.addComponent(RegionTeamCleaner, {
            region: new SphereRegion(
                DimensionIds.Overworld,
                { x: 34, y: -61, z: 49 },
                40
            ),
            teams: [this.chicken, this.shepherd],
        });
    }

    onUnload() {
        this.chicken.clear();
        this.shepherd.clear();
    }

    start() {
        Game.manager.startGame(CvSGame, {
            chicken: this.chicken,
            shepherd: this.shepherd,
            config: {
                map: this.map,
            },
        });
        const loader = this.getComponent(LazyLoader);
        this.runner.runDelay(() => {
            if (loader.isActive) {
                loader.reload();
            }
        }, 20);
    }
}
